/*
* File: CompositeTaskIdWidget.java
* Author: Daniel Rogers
* Created on Apr 15, 2008
*
*/
package gri.tasks.gui;
import java.awt.*;
import javax.swing.*;
import gri.tasks.managers.JobManagerSet;
import gri.tasks.managers.JobManagerEntry;
import gri.gui.widgets.Widget;
import gri.gui.widgets.options.ComboBoxWidget;
import gri.gui.widgets.options.Option;
import gri.gui.widgets.WidgetListener;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
public class TaskSelectionPanel extends JPanel {
JobManagerSet managers;
ComboBoxWidget cboManagers;
GroupedTaskInfoWidget taskWidget = null;
/**
* Map of TaskIdWidgets for each JobManager. Map is keyed
* by job manager ID.
*/
Map taskIdWidgets = new HashMap();
List taskSelectionListeners = new ArrayList();
// ------------------------------------------------- Constructors
public TaskSelectionPanel(JobManagerSet managers) {
super(new BorderLayout());
this.managers = managers;
init();
}
// ---------------------------------------------------- Accessors
public void addTaskSelectionListener(TaskSelectionListener listener) {
taskSelectionListeners.add(listener);
}
// ------------------------------------------------ Implementation
protected void init() {
//combo box:
cboManagers = new ComboBoxWidget();
List entries = managers.getJobManagerEntries();
JobManagerEntry entry;
for (int i=0; i<entries.size(); i++) {
entry = (JobManagerEntry)entries.get(i);
cboManagers.addOption(new Option(entry.getDisplayName(), entry));
}
cboManagers.addWidgetListener(new ManagerSelectListener());
add(cboManagers.getDisplayComponent(), BorderLayout.NORTH);
//panel:
if (entries.size() > 0)
cboManagers.setValue(entries.get(0));
}
/*
public void select(String id) {
}*/
public void selectManager(JobManagerEntry entry) {
String id = entry.getId();
GroupedTaskInfoWidget widget = (GroupedTaskInfoWidget)taskIdWidgets.get(id);
if (widget == null) {
widget = new GroupedTaskInfoWidget();
widget.addAllTasks(entry.getJobManager());
widget.addWidgetListener(new InnerListener());
taskIdWidgets.put(id, widget);
}
display(widget);
}
public void taskSelected(TaskInfo info) {
for (int i=0; i<taskSelectionListeners.size(); i++)
((TaskSelectionListener)taskSelectionListeners.get(i)).taskSelected(info);
}
/**
* Displays the widget in the center position of the panel,
* removing whatever component might presently be there.
*/
protected void display(GroupedTaskInfoWidget newWidget) {
//remove old:
if (taskWidget != null)
remove(taskWidget.getDisplayComponent());
taskWidget = newWidget;
//add new:
if (taskWidget != null)
add(taskWidget.getDisplayComponent(), BorderLayout.CENTER);
updateUI();
}
// ------------------------------------------------- Inner Classes
/**
* WidgetListener to convert valueChanged() events to taskSelected()
*/
class InnerListener implements WidgetListener {
public void valueChanged(Widget widget, Object value) {
if (value == null)
taskSelected(null);
if (value instanceof TaskInfo)
taskSelected((TaskInfo)value);
}
}
class ManagerSelectListener implements WidgetListener {
public void valueChanged(Widget widget, Object value) {
if (value == null)
return; //TODO:
if (value instanceof JobManagerEntry)
selectManager((JobManagerEntry)value);
else
throw new IllegalArgumentException("Expected widget value to be a JobManagerEntry, received: " + value.getClass().getName());
}
}
}